package com.ztk.trancation.service;

import com.ztk.trancation.mapper.TranscationMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/transcation")
public class TestRequireNew {

    @Autowired
    private TestRequireNewService testRequireNewService;

    private final SqlSessionFactory sqlSessionFactory;

    @Autowired
    public TestRequireNew(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }


    @GetMapping("/require_new")
    @Transactional(rollbackFor = Exception.class)
    public void requireNew(@RequestParam("date") String date, @RequestParam("count") int count) {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            TranscationMapper transcationMapper = sqlSession.getMapper(TranscationMapper.class);
            System.out.println("第一次查询date = " + date + ", count = " + count);
            Map<String, Object> select = transcationMapper.select(date, count);
            System.out.println(select.get("date"));
            testRequireNewService.requireNewUpdate(date, 0);
            sqlSession.clearCache();
            System.out.println("第二次查询date = " + date + ", count = " + count);
            Map<String, Object> select1 = transcationMapper.select(date, count);
            if (select1 == null) {
                System.out.println("select1的结果位空");
            } else {
                System.out.println(select1.get("date"));
            }
        }


    }

}
